    class Register{
        constructor(){
            this.user = document.getElementById("tel")
            this.pass = document.getElementById("pass1")
            this.btn = document.getElementById("reg_btn")
            this.span = document.getElementById("s")
			this.opass2=document.getElementById('pass2');
			this.ousername=document.getElementById('username');
			this.oidcard=document.getElementById('idcard');
			this.oyzm=document.getElementById('yzm');
			this.odxyzm=document.getElementById('dxyzm');
			this.oranyzm=document.getElementById('ranyzm');
			this.onoclear=document.getElementById('noclear');
			this.ra=false;
			this.rb=false;
			this.rc=false;
			this.rd=false;
			this.re=false;
			this.rf=false;
			this.rg=false;
			this.yzm()
			this.addEvent()
        }
        addEvent(){
            var that = this;
         
			this.user.onblur =function(){
				that.userP()
			}
			this.pass.onblur=function(){
				that.passP()
			}
			this.opass2.onblur=function(){
				that.pass2P()
			}
			this.ousername.onblur=function(){
				that.ousernameP()
			}
			this.oidcard.onblur=function(){
				that.oidcardP()
			}
			this.onoclear.onclick=function(){
				that.oyzm.value="";
				that.yzm()
			}
			this.oyzm.onblur=function(){
				var rege=that.oranyzm.innerHTML;
				var yzmV=that.oyzm.value;
				if(rege==yzmV){
					that.oyzm.parentNode.lastElementChild.innerHTML='<i id="green">格式正确</i>'
					that.rf=true;
				}else{
					that.oyzm.parentNode.lastElementChild.innerHTML='验证码有误'
					that.rf=false;
				}
			}
			this.btn.onclick = function(){
				if(that.ra && that.rb && that.rc && that.rd && that.re && that.rf){
					 that.setLocal()
				}
				else{
					that.user.onblur()
					that.oyzm.onblur();
					that.pass.onblur()
					that.opass2.onblur()
					that.ousername.onblur()
					that.oidcard.onblur()
				}
			   
			}
        }
		userP(){
			var regt=/^1[3578]\d{9}$/
			this.userMsg = localStorage.getItem("userMsg");
			this.userMsg = this.userMsg ? JSON.parse(this.userMsg) : [];
			    // 之前有，判断是否重名
			    var o = this.userMsg.some(val=>{
			        return val.user === this.user.value;
			    })
				if(this.user.value!=""){
					if(o){
					   this.user.nextElementSibling.innerHTML = "用户已存在，请"+"<a href='login.html' id='reg_dl'>登录</a>";
					   this.ra=false;
					   
					}else{
						if(regt.test(this.user.value)){
						    this.user.nextElementSibling.innerHTML = "<i id='green'>可以使用</i>";
							this.ra=true;
						}else{
							this.user.nextElementSibling.innerHTML='请填写正确手机号'
							this.ra=false;
						}
					}
					
				}else{
					this.user.nextElementSibling.innerHTML = "请输入用户名"
				}
			    
		}
		passP(){
			if(this.pass.value!=this.opass2.value&&this.opass2.value!=""){
				this.opass2.nextElementSibling.innerHTML='两次密码不一致'
				this.rc=false;
			}
			if(this.pass.value.length>=6&&this.pass.value.length<=20){
				var a=0;
				var b=0;
				var c=0;
				var regaa=/\d+/g;
				a=regaa.test(this.pass.value)?1:0;
				var regbb=/[a-zA-Z]+/g
				b=regbb.test(this.pass.value)?1:0;
				var regcc=/[^a-zA-Z\d]+/g
				c=regcc.test(this.pass.value)?1:0;
				var str='';
				switch(a+b+c){
					case 1:
					str='弱';break;
					case 2:
					str='中等';break;
					case 3:str='<i id="green">强</i>';
					break;
				}
				this.pass.nextElementSibling.innerHTML=str;
				this.rb=true;
			}else{
				this.pass.nextElementSibling.innerHTML='密码长度在6到20位之间'
				this.rb=false;
			}
		}
		
		pass2P(){
			if (this.opass2.value==""){
				this.opass2.nextElementSibling.innerHTML='确认密码不能为空'
				return;
			}
			if(this.pass.value==this.opass2.value){
				this.opass2.nextElementSibling.innerHTML='<i id="green">一致</i>'
				this.rc=true;
			}else{
				this.opass2.nextElementSibling.innerHTML='两次密码不一致'
				this.rc=false;
			}
		}
		ousernameP(){
			var reg1=/^[\u4e00-\u9fa5]{2,5}$/;
			var userObjV=this.ousername.value;
			if(reg1.test(userObjV)){
				this.ousername.nextElementSibling.innerHTML='<i id="green">姓名有效</i>';
				this.rd=true;//为了最后提交时判断,在这里提前写好
			}else{
				this.ousername.nextElementSibling.innerHTML='请填写真实姓名';
				this.rd=false;
			}
		}
		oidcardP(){
			var rege=/^\d{1}[\dXx]$/
			var idcardV=this.oidcard.value;
			if(rege.test(idcardV)){
				this.oidcard.nextElementSibling.innerHTML='<i id="green">格式正确</i>'
				this.re=true;
			}else{
				this.oidcard.nextElementSibling.innerHTML='请填写有效身份证'
				this.re=false;
			}
		}
		
		
		
		
		
		yzm(){
			var str=''
			for(var i=0;i<4;i++){
				var az=String.fromCharCode(random(122,97))
				var AZ=String.fromCharCode(random(90,65))
				var num=random(0,9)
				str+=az+AZ+num;
			}
			var yzm='';
			for(var j=0;j<4;j++){
				yzm+=str[random(0,str.length-1)]
			}
		
			function random(max,min){
				return Math.round(Math.random()*(max-min)+min);
			}
			this.oranyzm.innerHTML=yzm;
		}
		

        setLocal(){
            // 之前有没有
            this.userMsg = localStorage.getItem("userMsg");
            this.userMsg = this.userMsg ? JSON.parse(this.userMsg) : [];
    
            // 之前没有，直接存
            if(this.userMsg.length<1){
                this.userMsg.push({
                    user:this.user.value,
                    pass:this.pass.value,
                    onoff:0
                })
				this.span.innerHTML = "注册成功，2秒后进入登录页面";
				setTimeout(() => {
				    location.href = "login.html";
				}, 2000);
            }else{
                // 之前有，判断是否重名
                var o = this.userMsg.some(val=>{
                    return val.user === this.user.value;
                })
                if(o){
                    this.span.innerHTML = "用户已存在，请"+"<a href='login.html' id='reg_dl'>登录</a>";
                }else{
                    this.span.innerHTML = "注册成功，2秒后进入登录页面";
                    this.userMsg.push({
                        user:this.user.value,
                        pass:this.pass.value,
                        onoff:0
                    })
					setTimeout(() => {
					    location.href = "login.html";
					}, 2000);
                }
            }
    
            localStorage.setItem("userMsg",JSON.stringify(this.userMsg))
        }
    }
	
	new Register();